<?php


namespace app\app\controller;


use think\Db;

class Fx extends WxUser
{
    protected $FxInfo;
    protected $FxId;
    protected $Junior_member_ids;

    protected function initialize()
    {
        parent::initialize();
        $fx_info = Db::name('fx')->where([
            'member_id' => $this->memberId,
            'is_del' => 0
        ])->find();
        if (!$fx_info) {
            responseJson(-100, '陪诊信息不存在');
        }
        if ($fx_info['status'] != 1) {
            responseJson(-200, '状态异常，请联系管理员');
        }
        $this->Junior_member_ids = Db::name('member')->where([
            ['openid', '<>', ''],
            ['invite_id', '=', $this->memberId],
        ])->column('id');
        $this->FxInfo = $fx_info;
        $this->FxId = $fx_info['id'];
    }

    //分销账户中心
    public function fxFinanceTop()
    {
        //可提现
        $juniorMemberList = Db::name('member')->where([
            ['openid', '<>', ''],
            ['invite_id', '=', $this->memberId],
        ])->column('id');
        $detail_money1 = Db::name('server_order')->where([
            ['member_id', 'in', $juniorMemberList],
            ['status', 'in', [2, 3, 7]],
            ['is_del', '=', 0]
        ])->sum('fx_money');
        $detail_money = $detail_money1;
        //提现中
        $c = Db::name('member_finance')->where([
            ['type', '=', 4],
            ['is_kuan', '=', 0],
            ['member_id', '=', $this->memberId]
        ])->sum('money');
        //已提现
        $d = Db::name('member_finance')->where([
            ['type', '=', 4],
            ['is_kuan', '=', 1],
            ['member_id', '=', $this->memberId]
        ])->sum('money');
        $return['a'] = $this->FxInfo['money'];
        $return['b'] = $detail_money;
        $return['c'] = $c;
        $return['d'] = $d;
        msg(0, '查询成功', $return);
    }

    //分销提现
    public function fxTixian()
    {
        $data = $this->request->param();
        if ($data['amount'] <= 0) {
            msg(-1, '输入正确值', '');
        }

        if ($data['amount'] > $this->FxInfo['money']) {
            msg(-1, '提现金额不能大于余额', '');
        }
        Db::startTrans();
        try {
            $insert_data = array(
                'member_id' => $this->memberId,
                'money' => $data['amount'],
                'type' => 4,
                'tixian_type' => $data['tixian_type'], //1银行 2支付宝 3微信
                'tixian_account' => $data['tixian_account'], //提现账号
                'tixian_name' => $data['tixian_name'],//提现人姓名
                'tixian_bank' => $data['tixian_bank'],//提现银行名（银行提现使用）
                'desc' => '分销余额提现',
                'status' => 0,//'0未完成 待审核 1已完成 审核完成 提现使用',
                'create_time' => time(),
                'is_count' => 2
            );
            $insert_id = Db::name('member_finance')->insertGetId($insert_data);
            Db::name('fx')
                ->where('is_del', '=', 0)
                ->where('id', '=', $this->FxId)
                ->setDec('money', $data['amount']);
            Db::commit();
            msg(0, '更新成功', $insert_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //分销提现记录
    public function fxTixianList()
    {
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['member_id', '=', $this->memberId];
        $where[] = ['type', '=', 4];
        $data_list = Db::name('member_finance')
            ->where($where)
            ->order('create_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        }
        msg(0, '查询成功', $data_list);
    }

    //分销订单1 陪导诊订单列表
    public function order1List()
    {
        //1待支付2带指派3服务中4已完成5已取消6已退款7已指派8已评价'
        //1待支付5已取消6已退款  2带指派3服务中7已指派  4已完成8已评价
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);

        $where[] = ['status', 'in', [2, 3, 7, 4, 8]];
        $where[] = ['member_id', 'in', $this->Junior_member_ids];
        $where[] = ['pay_money', '>', 0];
        $where[] = ['fx_money', '>', 0];

        $data_count = Db::name('server_order')
            ->where($where)->count();
        $data_list = Db::name('server_order')
            ->where($where)
            ->order('create_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            if (in_array($v['status'], [2, 3, 7])) {
                $data_list[$k]['is_in'] = 0;
            } else {
                $data_list[$k]['is_in'] = 1;
            }
            $server_info = Db::name('server')->find($v['server_id']);
            $server_info['tag'] = explode(',', $server_info['tag']);
            $data_list[$k]['server_info'] = $server_info;
            $data_list[$k]['member_info'] = Db::name('member')->find($v['member_id']);
        }
        msg(0, '查询成功', $data_list, $data_count);
    }

    //陪诊卡分销订单
    public function order2List()
    {
        //'1待支付 2已支付 3已退款 4已取消',
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);

        $where[] = ['status', 'in', [2]];
        $where[] = ['member_id', 'in', $this->Junior_member_ids];
        $where[] = ['pay_money', '>', 0];
        $where[] = ['fx_money', '>', 0];

        $data_count = Db::name('package_order')
            ->where($where)->count();
        $data_list = Db::name('package_order')
            ->where($where)
            ->order('create_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            $data_list[$k]['is_in'] = 1;
            $package_info = Db::name('package')->find($v['package_id']);
            $package_info['icon_img']=$package_info['img'];
            $data_list[$k]['server_info'] = $package_info;
            $data_list[$k]['member_info'] = Db::name('member')->find($v['member_id']);
        }
        msg(0, '查询成功', $data_list, $data_count);
    }

    //分销我的下线
    public function myJunior()
    {
        $data = $this->request->param();
        $time_type = $data['time_type'];
        if ($time_type == 1) {
            $juniorMemberList = Db::name('member')->where([
                ['openid', '<>', ''],
                ['invite_id', '=', $this->memberId],
            ])->order('create_time desc')
                ->select();
        } else {
            $juniorMemberList = Db::name('member')->where([
                ['openid', '<>', ''],
                ['invite_id', '=', $this->memberId],
            ])->order('create_time')
                ->select();
        }
        foreach ($juniorMemberList as $k => $v) {
            $juniorMemberList[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            $juniorMemberList[$k]['order_num'] = Db::name('member_finance')->where([
                'junior_member_id' => $v['id'],
                'type' => 2,
                'is_count' => 1
            ])->count();
            $juniorMemberList[$k]['order_money'] = Db::name('member_finance')->where([
                'junior_member_id' => $v['id'],
                'type' => 2,
                'is_count' => 1
            ])->sum('money');
        }
        msg(0, '查询成功', $juniorMemberList);
    }
}